﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MySystemMag.Models;

namespace MySystemMag.DAL
{
    public class MenuFunction
    {
        public static List<MenuPower> GetMenuList(List<Menu> menulist)
        {
            var menupowerlist = new List<MenuPower>();

            foreach(var item in menulist)
            {
                var mp = new MenuPower();
                mp.id = item.MenuID;
                mp.pid = item.MenuParentID;
                mp.name = item.MenuNameCN;
                mp.url = item.MenuUrl;
                mp.icon = item.MenuIcon;
                mp.type = item.MenuType;
                mp.sort = item.MenuSort;
                mp.createDate = item.CreateDate;
                menupowerlist.Add(mp);
            }
            var mlist = GetMenu("0", menupowerlist);
            return mlist;
        }

        private static List<MenuPower> GetMenu(string pid, List<MenuPower> mplist = null)
        {
            var parent = mplist.Where(b => b.pid == pid);
            var lists= new List<MenuPower>();
            foreach(var item in parent)
            {
                var m = new MenuPower();
                m.id = item.id;
                m.pid = item.pid;
                m.name = item.name;
                m.url = item.url;
                m.icon = item.icon;
                m.type = item.type;
                m.sort = item.sort;
                m.createDate = item.createDate;
                m.children = GetSon(m, mplist);
                lists.Add(m);
            }
            return lists;
        }

        private static List<MenuPower> GetSon(MenuPower mp, List<MenuPower> mplist = null )
        {
            if (mplist.Exists(b => b.pid == mp.id))
            {
                return GetMenu(mp.id, mplist);
            }
            else
            {
                return null;
            }
        }
       
    }
}
